package com.biomatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biomatch.domain.Teacher;
import com.biomatch.domain.Type;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.Pagination;
import com.biomatch.dto.TeacherAndCourses;
import com.biomatch.mapper.TeacherMapper;
import com.biomatch.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;


    @Override
    public IPage<Teacher> getAllByPage(Pagination pagination) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("start",(pagination.getCurrent()-1) * pagination.getPageSize());
        params.put("pageSize",pagination.getPageSize());
        params.put("keywords",pagination.getKeywords());
        //获取总记录数据
        pagination.setTotal(teacherMapper.findCountByMap(params));
        IPage<Teacher> iPage = new Page<>(pagination.getCurrent(), pagination.getPageSize(),pagination.getTotal());
        iPage.setRecords(teacherMapper.selectTeacherList(params));
        return iPage;
    }

    @Override
    public List<Teacher> getAll() {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        return teacherMapper.selectList(queryWrapper);
    }

    @Override
    public Teacher saveOrUpdate(Teacher teacher) {
        //如果传入的id<=0 增加
        if(teacher.getId()<=0)
        {
            QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.eq("teacherName",teacher.getTeacherName());
            //如果有这个老师名字
            if(teacherMapper.selectCount(teacherQueryWrapper)>0)
            {
                return null;
            }
            //增加
            else{
                String code = String.valueOf(new Date().getTime());
                teacher.setTeacherId("t"+code.substring(4,code.length()));
                teacherMapper.insert(teacher);

                //查出所有舞种
                List<Type> addTypes=teacher.getTypes();
                //每条插入
                for (Type type:addTypes) {
                    teacherMapper.insertTeacherAndType(teacher.getTeacherId(),type.getDancerTypeId());
                }

                return teacher;
            }
        }
        //更新
        else {
            QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.eq("teacherName",teacher.getTeacherName())
                    .ne("teacherId",teacher.getTeacherId());
            if (teacherMapper.selectCount(teacherQueryWrapper) > 0) {
                return null;
            }
            else {
                teacherMapper.deleteTeacherAndTypes(teacher.getTeacherId());
                //查出所有舞种
                List<Type> addTypes=teacher.getTypes();
                //每条插入
                for (Type type:addTypes) {
                    teacherMapper.insertTeacherAndType(teacher.getTeacherId(),type.getDancerTypeId());
                }
                teacherMapper.updateById(teacher);
                return teacher;
            }
        }
    }

    @Override
    public Teacher delete(Teacher teacher) {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("teacherId",teacher.getTeacherId() );
        if (teacherMapper.selectCount(teacherQueryWrapper) > 0) {
            teacherMapper.deleteById(teacher.getId());
            teacherMapper.deleteTeacherAndTypes(teacher.getTeacherId());
            teacherMapper.deleteTeacherAndCourses(teacher.getTeacherId());
            return teacher;
        } else
        {
            return null;
        }
    }

    @Override
    public TeacherAndCourses addTeacherCourse(TeacherAndCourses teacherAndCourses) {
        //为空 不插入
        if(teacherAndCourses.getCourseId().size()==0&&teacherAndCourses.getCourseId().isEmpty())
        {
            return  null;
        }
        //根据已经有的课程加入老师课程里面
        else {
            List courses = teacherAndCourses.getCourseId();
            for (Object course: courses) {
                String courseId = (String)course;
                teacherMapper.insertTeacherAndCourse(teacherAndCourses.getTeacherId(),courseId);
            }
            return teacherAndCourses;
        }

    }
}
